package com.cuiweiyou.bluemesh.bluetooth;

import com.cuiweiyou.bluemesh.model.EventCodedModel;
import com.cuiweiyou.bluemesh.model.EventLightModel;
import com.cuiweiyou.bluemesh.model.LightModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class LightMaster {
    private static LightMaster instance;

    private String CODE_OPEN_ALL = "FBFFE7FFFF";
    private String CODE_CLOSE_ALL = "FBFFE7F000";
    private String CODE_ONE_OPEN; // "FB"+ CODE_ONE_DEVICE_ADDR + "E7FFFF";
    private String CODE_ONE_CLOSE;// "FB"+ CODE_ONE_DEVICE_ADDR + "E7F000";

    private LightModel singleLight;

    private LightMaster() {
        EventBus.getDefault().register(LightMaster.this);
    }

    public static LightMaster getInstance() {
        if (null == instance) {
            synchronized (LightMaster.class) {
                if (null == instance) {
                    instance = new LightMaster();
                }
            }
        }
        return instance;
    }

    public void release() {
        singleLight = null;
        EventBus.getDefault().unregister(LightMaster.this);
    }

    /**
     * 打开全部或关闭全部设备
     */
    public void openOrCloseAllLight(String group, boolean isAllOpen) {
        if (isAllOpen) {
            processAllLight(group, CODE_OPEN_ALL);
        } else {
            processAllLight(group, CODE_CLOSE_ALL);
        }
    }

    /**
     * 操作全部设备
     *
     * @param code kai或关的指令
     */
    private void processAllLight(String group, String code) {
        List<LightModel> devices = BluetoothMaster.getInstance().getDeviceListByGroup(group);
        if (null == devices || devices.size() < 1) {
            EventBus.getDefault().post(new EventLightModel(group, 0)); // 通知UI连接异常
            return;
        }

        LightModel target = null;
        List<LightModel> list = BluetoothMaster.getInstance().getDeviceListByGroup(group);
        for (int i = 0; i < list.size(); i++) {
            LightModel light = list.get(i);
            if (light.isOnline()) {
                if (null == target) {
                    target = light;
                } else {
                    if (light.getAvgMile() < target.getAvgMile()) {
                        target = light;
                    }
                }
            }
        }
        if (null == target) { // 找一个小管理员。距离最近的那个
            EventBus.getDefault().post(new EventLightModel(group, -1)); // 通知UI连接异常
            return;
        }

        flushLight(target, code);
    }

    /**
     * 操作某个设备
     *
     * @param model
     */
    public void flushLight(LightModel model) {
        flushLight(model, null);
    }

    /**
     * 发令
     *
     * @param model   执行任务的设备/目标设备。
     * @param theCode 指令
     */
    private void flushLight(LightModel model, String theCode) {
        singleLight = model;

        String mac = singleLight.getAddress();
        String addr = mac.substring(mac.length() - 2);

        String code;
        if (null == theCode) {
            if (singleLight.isOpen()) {
                code = "FB" + addr + "E7F000";
            } else {
                code = "FB" + addr + "E7FFFF";
            }
        } else {
            code = theCode;
        }

        BluetoothMaster.getInstance().processDevice(model, code);
    }

    // 指令发送反馈
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onCodedEvent(EventCodedModel model) { // 收到消息：指令发送完毕
        if (null != model) {
            int status = model.getStatus();
            if (0 == status) {
                if (model.getCode().equals(CODE_OPEN_ALL)) {
                    BluetoothMaster.getInstance().flushDevices(singleLight.getName(), 1); // 设备全开
                } else if (model.getCode().equals(CODE_CLOSE_ALL)) {
                    BluetoothMaster.getInstance().flushDevices(singleLight.getName(), 0); // 设备全关
                } else {
                    singleLight.setOpen(!singleLight.isOpen()); //
                }
            }
            EventBus.getDefault().post(new EventLightModel(singleLight.getName(), 1)); // 通知UI更新设备状态
        }
    }
}
